package com.dankegongyu.lib.common.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.telephony.TelephonyManager;

import androidx.fragment.app.FragmentActivity;

/**
 * https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/main/java/com/blankj/utilcode/util/PhoneUtils.java
 */
public final class PhoneUtil {

    private PhoneUtil() {
        throw new AssertionError("cannot be instantiated");
    }

    /**
     * 获取IMEI码
     * <p>需添加权限 {@code <uses-permission android:name="android.permission.READ_PHONE_STATE"/>}</p>
     *
     * @return IMEI码
     */
    @SuppressLint({"HardwareIds", "MissingPermission"})
    public static String getIMEI() {
        String imei = null;
        try {
            TelephonyManager tm = (TelephonyManager) GlobalContext.getAppContext().getSystemService(Context.TELEPHONY_SERVICE);
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
                if (tm.hasCarrierPrivileges()) {
                    imei = tm.getImei();
                }
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                imei = tm.getImei();
            } else {
                imei = tm.getDeviceId();
            }
        } catch (Exception e) {
//            e.printStackTrace();
        }
        return imei == null ? "" : imei;
    }

    /**
     * 获取IMSI码
     * <p>需添加权限 {@code <uses-permission android:name="android.permission.READ_PHONE_STATE"/>}</p>
     *
     * @return IMSI码
     */
    @SuppressLint({"HardwareIds", "MissingPermission"})
    public static String getIMSI() {
        String imsi = "";
        try {
            TelephonyManager tm = (TelephonyManager) GlobalContext.getAppContext().getSystemService(Context.TELEPHONY_SERVICE);
            //noinspection ConstantConditions
            imsi = tm.getSubscriberId();
        } catch (Exception e) {
//            e.printStackTrace();
        }
        return imsi == null ? "" : imsi;
    }

    /**
     * 跳至拨号界面
     *
     * @param phoneNumber 电话号码
     */
    public static void dial(Context context, final String phoneNumber) {
        try {
            Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 拨打电话
     * <p>需添加权限 {@code <uses-permission android:name="android.permission.CALL_PHONE"/>}</p>
     *
     * @param phoneNumber 电话号码
     */
    @SuppressLint("MissingPermission")
    public static void call(final Context context, final String phoneNumber) {
        if (context instanceof FragmentActivity) {
            DKPermission.requestCallPhonePermission((FragmentActivity) context, new DKPermission.Callback() {
                @Override
                public void onResult(boolean granted) {
                    realCall(context, phoneNumber);
                }
            });
        }
    }

    private static void realCall(Context context, final String phoneNumber) {
        try {
            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
